package com.macro.mall.service.impl;

import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.macro.mall.mapper.UmsMemberMapper;
import com.macro.mall.mapper.UmsMemberStatisticsInfoMapper;
import com.macro.mall.model.UmsMember;
import com.macro.mall.model.UmsMemberExample;
import com.macro.mall.model.UmsMemberStatisticsInfo;
import com.macro.mall.model.UmsMemberStatisticsInfoExample;
import com.macro.mall.service.UmsMemberCacheService;
import com.macro.mall.service.UmsMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.util.List;
@Service
public class UmsMemberServiceImpl implements UmsMemberService {
    @Autowired
    private UmsMemberMapper umsMemberMapper;
    @Autowired
    private UmsMemberCacheService umsMemberCacheService;
    @Autowired
    private PasswordEncoder passwordEncoder;
    @Autowired
    private UmsMemberStatisticsInfoMapper umsMemberStatisticsInfoMapper;
    @Override
    public List<UmsMember> list(String keyword, Integer pageSize, Integer pageNum) {
        PageHelper.startPage(pageNum, pageSize);
        UmsMemberExample example = new UmsMemberExample();
        UmsMemberExample.Criteria criteria=example.createCriteria();
        if (!StringUtils.isEmpty(keyword)) {
            criteria.andUsernameLike("%" + keyword + "%");
            example.or(example.createCriteria().andNicknameLike("%" + keyword + "%"));
        }
        return umsMemberMapper.selectByExample(example);
    }

    @Override
    public int delete(Long id) {
        umsMemberCacheService.delMember(id);
        return umsMemberMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int update(UmsMember umsMember) {
        UmsMember member = umsMemberMapper.selectByPrimaryKey(umsMember.getId());
//        if(member.getPassword().equals(umsMember.getPassword())){
//            //与原加密密码相同的不需要修改
//            umsMember.setPassword(null);
//        }else{
            //与原加密密码不同的需要加密修改
//            if(StrUtil.isEmpty(umsMember.getPassword())){
//                umsMember.setPassword(null);
//            }else{
//                umsMember.setPassword(passwordEncoder.encode(umsMember.getPassword()));
//            }
//        }
        int i = umsMemberMapper.updateByPrimaryKeySelective(umsMember);
        umsMemberCacheService.delMember(umsMember.getId());
        return i;
    }

    @Override
    public UmsMember getById(Long id) {
        return umsMemberMapper.selectByPrimaryKey(id);
    }

    @Override
    public UmsMemberStatisticsInfo statisticsGetById(Long id) {
        UmsMemberStatisticsInfoExample example = new UmsMemberStatisticsInfoExample();
        example.createCriteria().andMemberIdEqualTo(id);
        UmsMemberStatisticsInfo umsMemberStatisticsInfo = new UmsMemberStatisticsInfo();
        umsMemberStatisticsInfo.setConsumeAmount(new BigDecimal(0.00));
        umsMemberStatisticsInfo.setOrderCount(0);
        umsMemberStatisticsInfo.setCouponCount(0);
        umsMemberStatisticsInfo.setCommentCount(0);
        umsMemberStatisticsInfo.setReturnOrderCount(0);
        umsMemberStatisticsInfo.setLoginCount(0);
        umsMemberStatisticsInfo.setAttendCount(0);
        umsMemberStatisticsInfo.setFansCount(0);
        umsMemberStatisticsInfo.setCollectCommentCount(0);
        umsMemberStatisticsInfo.setCollectProductCount(0);
        umsMemberStatisticsInfo.setCollectSubjectCount(0);
        umsMemberStatisticsInfo.setCollectTopicCount(0);
        List<UmsMemberStatisticsInfo> umsMemberStatisticsInfos = umsMemberStatisticsInfoMapper.selectByExample(example);
        if (umsMemberStatisticsInfos.isEmpty()){
            return umsMemberStatisticsInfo;
        }
        umsMemberStatisticsInfo=umsMemberStatisticsInfos.get(0);
        return umsMemberStatisticsInfo;
    }
}
